package com.jlkf.service.util;

import org.json.*;
import java.io.*;
import java.net.*;

//pushwoosh推送
public class SendPushNotification {
	    public static final String PUSHWOOSH_SERVICE_BASE_URL = "https://cp.pushwoosh.com/json/1.3/";
	    private static final String AUTH_TOKEN = "MHMUzO0ZSH01x1fhKW06AXEacJodgIwYGWdi801qMn8jGMcTtCdnBrAxXo1MbktYEMFN7hFY8ro4ssG8piYi";
	    private static final String APPLICATION_CODE_USER = "142BF-1A2D0";
	    private static final String APPLICATION_CODE_DRIVER = "0521C-62778";//司机应用
	    //
	    public static void main(String[] args) throws JSONException, MalformedURLException
	    {
	      /*  String method = "createMessage";
	        URL url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
	        JSONObject aps = new JSONObject().put("content-available", "testttttttttttttttttttttt").put("mutable-content", 1);
	        JSONObject   iosRoot = new JSONObject().put("aps", aps);
	        JSONObject   iosRoot1 = new JSONObject().put("安卓", "安卓");
	        JSONArray  notificationsArray = new JSONArray()
		               .put(new JSONObject().put("send_date", "now")
                               .put("content", "test23")
                               .put("android_silent", "1")
                               .put("android_gcm_ttl", 3600)
                               .put("android_vibration", 0)
                               .put("android_root_params", iosRoot1)
                               .put("users","user1517557148777"));
	       System.out.println("notificationsArray="+notificationsArray);
	        JSONArray notificationsArray = new JSONArray()
	                .put(new JSONObject().put("send_date", "now")
	                                     .put("content", "test23")
	                                     .put("users","23"));
	 // .put("link", "http://pushwoosh.com/")
	        JSONObject requestObject = new JSONObject()
	                .put("application", APPLICATION_CODE_USER)
	                .put("auth", AUTH_TOKEN)
	                .put("notifications", notificationsArray);
	 
	        JSONObject mainRequest = new JSONObject().put("request", requestObject);
	        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());
	        System.out.println("Response is: " + response);*/
	    	
//	    	sendMessageAlertUser("user1517557148777", "安卓");
//	    	sendMessageSilentUser("user1517562636459", "{\"type\":3,\"status\":2}", "andriod");
//	    	sendMessageSilentDriver("user1516677989350", "{\"type\":3,\"status\":2}", "ios");
	    	sendMessageSilentUser1("driver1520041873846", "{\"type\":3,\"status\":2}","ios");
	    }
	    //推送通知 alert
	    public static void  sendMessageAlertDriver(String userId,String content){
	    	 String method = "createMessage";
		        URL url;
				try {
					url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
					JSONArray notificationsArray = new JSONArray()
			                .put(new JSONObject().put("send_date", "now")
			                                     .put("content", content)
			                                     .put("users", userId));
			 
			        JSONObject requestObject = new JSONObject()
			                .put("application", APPLICATION_CODE_DRIVER)
			                .put("auth", AUTH_TOKEN)
			                .put("notifications", notificationsArray);
			 
			        JSONObject mainRequest = new JSONObject().put("request", requestObject);
			        System.out.println("sendMessageAlertDriver——requestObject="+requestObject);
			        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
			        
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    }
	    //自定义 静默
	    public static void sendMessageSilentDriver(String userId,String content,String aurora_alias){
	   	 String method = "createMessage";
		        URL url;
				try {
					url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
					 JSONArray notificationsArray =null;
					 JSONArray iosplatforms =new JSONArray().put(1);
					 JSONArray androidplatforms =new JSONArray().put(3);
					 if("ios".equals(aurora_alias)){
						 JSONObject aps = new JSONObject().put("content-available", content).put("mutable-content", 1);
					        JSONObject   iosRoot = new JSONObject().put("aps", aps);
						  notificationsArray = new JSONArray()
					                .put(new JSONObject().put("send_date", "now")
					                                     .put("ios_silent", "1")
					                                     .put("platforms", iosplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
					                                     .put("ios_ttl", 3600)
					                                     .put("ios_root_params", iosRoot)
					                                     .put("users",userId));
						  //  
					 }else{
						  notificationsArray = new JSONArray()
					               .put(new JSONObject().put("send_date", "now")
					                                     .put("android_silent", "3")
					                                     .put("platforms",androidplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
					                                     .put("android_gcm_ttl", 3600)
					                                     .put("android_vibration", 0)
					                                     .put("android_root_params", content)
					                                     .put("users",userId));
					 }
					 
				       System.out.println("notificationsArray="+notificationsArray);;
			        JSONObject requestObject = new JSONObject()
			        		 .put("application", APPLICATION_CODE_DRIVER)
				                .put("auth", AUTH_TOKEN)
			                .put("notifications", notificationsArray);
			        JSONObject mainRequest = new JSONObject().put("request", requestObject);
			        System.out.println("sendMessageSilentDriver——requestObject="+requestObject);
			        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
			        
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	   }
	    //订单是带声音的自定义推送
	    public static void sendOrderMessageSilentDriver(String userId,String content,String aurora_alias){
		   	 String method = "createMessage";
			        URL url;
					try {
						url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
						 JSONArray notificationsArray =null;
						 JSONArray iosplatforms =new JSONArray().put(1);
						 JSONArray androidplatforms =new JSONArray().put(3);
						 if("ios".equals(aurora_alias)){
							  notificationsArray = new JSONArray()
						                .put(new JSONObject().put("send_date", "now")
			                                     .put("content", content)
			                                     .put("ios_sound", "trumpety.wav")//音频
			                                     .put("users", userId));
							  //  
						 }else{
							 notificationsArray = new JSONArray()
						                .put(new JSONObject().put("send_date", "now")
			                                     .put("content", content)
			                                     .put("android_sound", "trumpety.wav")//音频
			                                     .put("users", userId));
							 /* notificationsArray = new JSONArray()
						               .put(new JSONObject().put("send_date", "now")
						                                     .put("android_silent", "3")
						                                     .put("android_sound", "trumpety.wav")//音频
						                                     .put("platforms",androidplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
						                                     .put("android_gcm_ttl", 3600)
						                                     .put("android_vibration", 0)
						                                     .put("android_root_params", content)
						                                     .put("users",userId));*/
						 }
						 
					       System.out.println("notificationsArray="+notificationsArray);;
				        JSONObject requestObject = new JSONObject()
				        		 .put("application", APPLICATION_CODE_DRIVER)
					                .put("auth", AUTH_TOKEN)
				                .put("notifications", notificationsArray);
				        JSONObject mainRequest = new JSONObject().put("request", requestObject);
				        System.out.println("sendOrderMessageSilentDriver——requestObject="+requestObject);
				        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
				        
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		   }
	    //取消是带声音的自定义推送
	    public static void sendMessageDriverCancel(String userId,String content,String aurora_alias){
		   	 String method = "createMessage";
			        URL url;
					try {
						url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
						 JSONArray notificationsArray =null;
						 JSONArray iosplatforms =new JSONArray().put(1);
						 JSONArray androidplatforms =new JSONArray().put(3);
						 if("ios".equals(aurora_alias)){
							  notificationsArray = new JSONArray()
						                .put(new JSONObject().put("send_date", "now")
			                                     .put("content", content)
			                                     .put("ios_sound", "driverCancelled.wav")//音频
			                                     .put("users", userId));
							  //  
						 }else{
							 notificationsArray = new JSONArray()
						                .put(new JSONObject().put("send_date", "now")
			                                     .put("content", content)
			                                     .put("android_sound", "driverCancelled.wav")//音频
			                                     .put("users", userId));
							 /* notificationsArray = new JSONArray()
						               .put(new JSONObject().put("send_date", "now")
						                                     .put("android_silent", "3")
						                                     .put("android_sound", "trumpety.wav")//音频
						                                     .put("platforms",androidplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
						                                     .put("android_gcm_ttl", 3600)
						                                     .put("android_vibration", 0)
						                                     .put("android_root_params", content)
						                                     .put("users",userId));*/
						 }
						 
					       System.out.println("notificationsArray="+notificationsArray);;
				        JSONObject requestObject = new JSONObject()
				        		 .put("application", APPLICATION_CODE_DRIVER)
					                .put("auth", AUTH_TOKEN)
				                .put("notifications", notificationsArray);
				        JSONObject mainRequest = new JSONObject().put("request", requestObject);
				        System.out.println("sendOrderMessageSilentDriver——requestObject="+requestObject);
				        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
				        
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		   }
	    
   //推送通知 alert
    public static void  sendMessageAlertUser(String userId,String content){
    	 String method = "createMessage";
	        URL url;
			try {
				url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
				JSONArray notificationsArray = new JSONArray()
		                .put(new JSONObject().put("send_date", "now")
		                                     .put("content", content)
		                                     .put("users", userId));
		 
		        JSONObject requestObject = new JSONObject()
		                .put("application", APPLICATION_CODE_USER)
		                .put("auth", AUTH_TOKEN)
		                .put("notifications", notificationsArray);
		 
		        JSONObject mainRequest = new JSONObject().put("request", requestObject);
		        System.out.println("sendMessageAlertUser——requestObject="+requestObject);
		        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
		        
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    }
    //自定义 静默
    public static void sendMessageSilentUser(String userId,String content,String aurora_alias){
   	 String method = "createMessage";
	        URL url;
			try {
				
				url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
				 JSONArray notificationsArray =null;
				 JSONArray iosplatforms =new JSONArray().put(1);
				 JSONArray androidplatforms =new JSONArray().put(3);
				 if("ios".equals(aurora_alias)){
					 JSONObject aps = new JSONObject().put("content-available", content).put("mutable-content", 1);
				        JSONObject   iosRoot = new JSONObject().put("aps", aps);
					  notificationsArray = new JSONArray()
				                .put(new JSONObject().put("send_date", "now")
				                                     .put("platforms", iosplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
				                                     .put("ios_ttl", 3600)
				                                     .put("ios_root_params", iosRoot)
				                                     .put("users",userId));
				 }else{
					  notificationsArray = new JSONArray()
				               .put(new JSONObject().put("send_date", "now")
				                                     .put("android_silent", "1")
				                                     .put("platforms",androidplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
				                                     .put("android_gcm_ttl", 3600)
				                                     .put("android_vibration", 0)
				                                     .put("android_root_params", content)
				                                     .put("users",userId));
				 }
				 
			       System.out.println("notificationsArray="+notificationsArray);;
		        JSONObject requestObject = new JSONObject()
		        		 .put("application", APPLICATION_CODE_USER)
			                .put("auth", AUTH_TOKEN)
		                .put("notifications", notificationsArray);
		        JSONObject mainRequest = new JSONObject().put("request", requestObject);
		        System.out.println("sendMessageSilentUser——requestObject="+requestObject);
		        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
		        
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
   }
    
    
    public static void sendMessageSilentUser1(String userId,String content,String aurora_alias){
     	 String method = "createMessage";
   	        URL url;
   			try {
   				
   				url = new URL(PUSHWOOSH_SERVICE_BASE_URL + method);
   				 JSONArray notificationsArray =null;
   				 JSONArray iosplatforms =new JSONArray().put(1);
   				 JSONArray androidplatforms =new JSONArray().put(3);
   				 if("ios".equals(aurora_alias)){
   					 JSONObject aps = new JSONObject().put("content-available", content).put("mutable-content", 1);
				        JSONObject   iosRoot = new JSONObject().put("aps", aps);
					  notificationsArray = new JSONArray()
				                .put(new JSONObject().put("send_date", "now")
				                                     .put("ios_silent", "1")
				                                     .put("platforms", iosplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
				                                     .put("ios_ttl", 3600)
				                                     .put("ios_root_params", iosRoot));
//				                                     .put("users",userId)
   				 }else{
   					  notificationsArray = new JSONArray()
   				               .put(new JSONObject().put("send_date", "now")
   				                                     .put("android_silent", "1")
   				                                     .put("platforms",androidplatforms)// 1 — iOS; 2 — BB; 3 — Android; 5 — Windows Phone; 7 — OS X; 8 — Windows 8; 9 — Amazon; 10 — Safari; 11 — Chrome; 12 — Firefox; ignored if "devices" < 10
   				                                     .put("android_gcm_ttl", 3600)
   				                                     .put("android_vibration", 0)
   				                                     .put("android_root_params", content));
//   				                                     .put("users",userId));
   				 }
   				 
   			       System.out.println("notificationsArray="+notificationsArray);;
   		        JSONObject requestObject = new JSONObject()
   		        		 .put("application", APPLICATION_CODE_DRIVER)
   			                .put("auth", AUTH_TOKEN)
   		                .put("notifications", notificationsArray);
   		        JSONObject mainRequest = new JSONObject().put("request", requestObject);
   		        System.out.println("requestObject="+requestObject);
   		        JSONObject response = SendServerRequest.sendJSONRequest(url, mainRequest.toString());//发送请求
   		        
   			} catch (MalformedURLException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
     }
   }
    

	class SendServerRequest
	{
	    static JSONObject sendJSONRequest(URL url, String request)
	    {
	        HttpURLConnection connection = null;
	        try
	        {
	            connection = (HttpURLConnection) url.openConnection();
	            connection.setRequestMethod("POST");
	            connection.setRequestProperty("Content-Type", "application/json");
	            connection.setDoInput(true);
	            connection.setDoOutput(true);
	 
	            DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
	            System.out.println("----------"+request.getBytes("UTF-8"));
	            writer.write(request.getBytes("UTF-8"));
	            writer.flush();
	            writer.close();
	 
	            return parseResponse(connection);
	        }
	        catch (Exception e)
	        {
	            System.out.println("An error occurred: " + e.getMessage());
	            return null;
	        }
	        finally
	        {
	            if (connection != null)
	            {
	                connection.disconnect();
	            }
	        }
	    }
	 
	    static JSONObject parseResponse(HttpURLConnection connection) throws IOException, JSONException
	    {
	        String line;
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        StringBuilder response = new StringBuilder();
	 
	        while ((line = reader.readLine()) != null)
	        {
	            response.append(line).append('\r');
	        }
	        System.out.println("response="+response);
	        reader.close();
	 
	        return new JSONObject(response.toString());
	    }
	}